package dancrawler.server.events;

import dancrawler.common.Misc;
import dancrawler.server.GameHandler;
import dancrawler.server.Player;
import dancrawler.server.Server;
import dancrawler.server.netMsg;

/**
 *
 */
public class DisconnectHandler implements EventHandler {

    private Server server;
    private GameHandler gh;

    public DisconnectHandler(Server server, GameHandler gh) {
        this.server = server;
        this.gh = gh;
    }

    public void handleEvent(DisconnectEvent ev) {
        if (gh.getPlayer(ev.getClientId()) != null) {
            Player pl1 = gh.getPlayer(ev.getClientId());
            int pid = gh.getPlayer(ev.getClientId()).getId();
            pl1.getPc().getWorld().removeObject(gh.getPlayerCharacter(ev.getClientId()));
            gh.removePlayer(gh.getPlayer(ev.getClientId()));
            for (Player pl : gh.getPlayers()) {
                netMsg msg = new netMsg((short) 15, (short) 0, null);
                byte[][] args = new byte[1][];
                msg.setArgs(args);
                args[0] = Misc.shortToBytes((short)pid);
                msg.setFinalized(true);
                pl.getCon().getOutQueue().offer(msg);
            }
        }
    }
}
